package com.xzw.cameratest.calendardialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.xzw.cameratest.R;

import java.util.Calendar;
import java.util.List;


/**
 * Created by 46085 on 2019/9/6.
 */

public class CalendarRVAdapter extends RecyclerView.Adapter<CalendarRVAdapter.ViewHolder> {

    private Context context;
    private List<DateBean> dateBeans;
    private OnItemClickListener onItemClickListener;
    private Calendar calendarNow;

    public CalendarRVAdapter (Context context) {
        super();
        this.context = context;
        this.calendarNow = (Calendar) Calendar.getInstance().clone();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_calendar_dialog_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final DateBean dateBean = dateBeans != null ? dateBeans.get(position) : null;
        if (dateBean != null && dateBean.isEnable()) {

            if (dateBean.getYear() == calendarNow.get(Calendar.YEAR)
                    && dateBean.getMonth() == calendarNow.get(Calendar.MONTH)
                    && dateBean.getDay() == calendarNow.get(Calendar.DAY_OF_MONTH)) {
                holder.rly_item_calendar_date.setBackground(ContextCompat.getDrawable(context, R.drawable.calendar_dialog_item_bg_selector_today));
                holder.tv_item_calendar_date.setTextColor(ContextCompat.getColor(context, dateBean.isSelect() ? R.color.C0 : R.color.C9));
            } else if (dateBean.getDayOfWeek() == Calendar.SUNDAY || dateBean.getDayOfWeek() == Calendar.SATURDAY) {
                holder.rly_item_calendar_date.setBackground(ContextCompat.getDrawable(context, R.drawable.calendar_dialog_item_bg_selector_weekend));
                holder.tv_item_calendar_date.setTextColor(ContextCompat.getColor(context, dateBean.isSelect() ? R.color.C0 : R.color.C3));
            } else {
                holder.rly_item_calendar_date.setBackground(ContextCompat.getDrawable(context, R.drawable.calendar_dialog_item_bg_selector_normal));
                holder.tv_item_calendar_date.setTextColor(ContextCompat.getColor(context, dateBean.isSelect() ? R.color.C0 : R.color.C1));
            }

            holder.tv_item_calendar_date.setText(dateBean.getShownDay());
            holder.iv_item_calendar_date_had_record.setVisibility(dateBean.isHadRecord() ? View.VISIBLE : View.INVISIBLE);
            holder.itemView.setSelected(dateBean.isSelect());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    updateSelectDateBean(dateBean);
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(holder.getAdapterPosition(), dateBean);
                    }
                }
            });
        } else {
            holder.itemView.setSelected(false);
        }
    }

    @Override
    public int getItemCount() {
        return dateBeans == null ? 0 : dateBeans.size();
    }

    public void updateSelectDateBean (DateBean dateBean) {
        for (DateBean dateBeanTemp : dateBeans) {
            dateBeanTemp.setSelect(false);
        }
        dateBean.setSelect(true);
        notifyDataSetChanged();
    }

    public void setDateBeans(List<DateBean> dateBeans) {
        this.dateBeans = dateBeans;
    }

    public void setCalendarNow(Calendar calendarNow) {
        this.calendarNow = (Calendar) calendarNow.clone();
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, DateBean dateBean);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout rly_item_calendar_date;
        TextView tv_item_calendar_date;
        ImageView iv_item_calendar_date_had_record;

        public ViewHolder(View itemView) {
            super(itemView);
            rly_item_calendar_date = itemView.findViewById(R.id.rly_item_calendar_date);
            tv_item_calendar_date = itemView.findViewById(R.id.tv_item_calendar_date);
            iv_item_calendar_date_had_record = itemView.findViewById(R.id.iv_item_calendar_date_had_record);
        }
    }
}
